package com.example.onlineeducation.ui.mine

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.databinding.StatisticsStudyTodayAcBinding
import com.example.onlineeducation.databinding.StatisticsStudyTotalAcBinding
import com.example.onlineeducation.utils.YcTimeUtils
import com.example.onlineeducation.viewmodel.StatisticsStudyTotalAcVM
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.data.*
import com.github.mikephil.charting.formatter.ValueFormatter
import com.github.mikephil.charting.utils.ViewPortHandler
import com.yc.jetpacklib.extension.*
import com.yc.jetpacklib.net.doFail
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.utils.YcTime
import com.yc.jetpacklib.widget.chart.YcChartFiniteLength
import com.yc.jetpacklib.widget.chart.YcChartMarkerView
import com.yc.jetpacklib.widget.chart.YcEntry
import com.yc.jetpacklib.widget.chart.YcMarkerEntity

/**
 * 学习统计-今日学习统计
 */
@SuppressLint("SetTextI18n")
class StatisticsStudyTodayAc : BaseActivity<StatisticsStudyTodayAcBinding>(StatisticsStudyTodayAcBinding::inflate) {
    companion object {
        @JvmStatic
        fun newInstance(context: Context) {
            context.startActivity(Intent(context, StatisticsStudyTodayAc::class.java))
        }
    }

    private val mViewModel by ycViewModels<StatisticsStudyTotalAcVM>()
    override fun StatisticsStudyTodayAcBinding.initView() {
        actionBar.setTitle("今日学习统计")
        App.mInstant.mViewModel.mProInfoEntity.observe {
            val timeStart = "${YcTime.getCurrentTime(YcTime.FORMAT_TIME)} 00:00:00"
            val timeEnd = "${YcTime.getCurrentTime(YcTime.FORMAT_TIME)} 23:59:59"
            mViewModel.initData(it!!.proId,timeStart,timeEnd)
        }

        pcStudyType.ycChartInitDefault()
        pcStudyType.setNoDataText("今日暂无数据")
        pcStudyType.minOffset = 40f
        mViewModel.apply {
            mUserProStudyTime.observe {
                it.doSuccess { entity ->
                    tvStudyTimeToday.text = entity.todayStudyTime
                }.ycShowNetError()
            }
            mUserProStudyChar.observe {
                it.doSuccess {
                    pcStudyType.refreshChartInfo(it.studyType)
                }.doFail {
                    pcStudyType.refreshChartInfo(null)
                }.ycShowNetError()
            }
        }

    }


    private fun PieChart.refreshChartInfo(studyWeekType: List<PieEntry>?) {
        if (studyWeekType.ycIsEmpty()) {
            clear()
        } else {
            ycChartSetLineDataSet(studyWeekType!!){
                it.label
            }
        }
        ycChartRefresh()//刷新图表
    }
}